import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_html/flutter_html.dart';
import '../../common/ScreenAdaper.dart';
import 'package:dio/dio.dart';
import '../../config/Config.dart';
import '../../widget/Tool.dart';
import '../../services/Storage.dart';

class ContentPage extends StatefulWidget {
  Map arguments;
  ContentPage({Key key,this.arguments}) : super(key: key);
  _ContentPageState createState() => _ContentPageState();
}

class _ContentPageState extends State<ContentPage> {
  ScrollController _controller = new ScrollController();
  Map _data = {};
  String _token = "";

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _getDate();
    _getToken();
  }

  // 获取章节信息
  _getDate() async{
    var apiUrl = "${Config.domain}api/book/article?bookid=${widget.arguments["bookId"]}&articleid=${widget.arguments["articleId"]}";
    Response result=await Dio().get(apiUrl);
    var list=result.data;
    if(list['code'] == 1){
      setState(() {
        this._data = list["data"];
      });
    }
  }
  _getToken() async{
    try{
      String token = await Storage.getString("token");
      setState(() {
        this._token=token;
      });
    } catch (e){
      print(e);
    }
  }
  // 加入书签
  _addBookCase(int BookId,int ArticleId,String EndArticleName) async{
    var apiUrl = "${Config.domain}api/book/bookmark_add?token=${_token}";
    Response result=await Dio().post(apiUrl,data: {
      "BookId":BookId,
      "ArticleId":ArticleId,
      "EndArticleName":EndArticleName,
    });
    var list=result.data;
    Tool.ShowToast(list["msg"]);
  }

  @override
  Widget build(BuildContext context) {
    S.init(context);
    return Scaffold(
        appBar: AppBar(
          title: Text("${_data.length>0 ? _data["Name"] : " "}"),
          leading: GestureDetector(
            child: Icon(Icons.arrow_back,color: Colors.black54),
            onTap: (){
              Navigator.pop(context);
            },
          ),
          actions: <Widget>[
            Container(
              padding: EdgeInsets.only(right: 10),
              child: GestureDetector(
                child: Icon(Icons.home,color: Colors.black54),
                onTap: (){
                  Navigator.pushNamed(context, "/index");
                },
              ),
            )
          ],
        ),
        floatingActionButton: FloatingActionButton(
          child: Icon(Icons.arrow_upward),
          mini:true,
          onPressed: () {
            _controller.animateTo(.0,
                duration: Duration(milliseconds: 200),
                curve: Curves.ease
            );
          },
        ),
        body: Scrollbar(
          child: SingleChildScrollView(
            controller: _controller,
            child: Column(
              children: <Widget>[
                _data.length > 0 ? _content() : _load(),
              ],
            ),
          ),
        )
    );
  }

  Widget _load(){
    return Container(
      color: Color.fromARGB(255, 220, 236, 210),
      width: double.infinity,
      height: S.getScreenHeight(),
      child: Center(
        child: Text("加载中。。"),
      ),
    );
  }

  Widget _content(){
    return Container(
      color: Color.fromARGB(255, 220, 236, 210),
      padding: EdgeInsets.only(bottom: S.width(120)),
      child: Column(
        children: <Widget>[
          Container(
            padding: EdgeInsets.only(top: S.height(20)),
            child: Center(
              child: Text(
                "${_data.length>0 ? "《${_data["Name"]}》" : " "}",
                style: TextStyle(fontWeight: FontWeight.bold,fontSize: S.size(38),color: Colors.black87),
              ),
            ),
          ),
          _navi(),
          Container(
            padding: EdgeInsets.all(S.width(10)),
            child: Html(
              data: "${_data["Body"]}",
            ),
          ),
          _navi(),
        ],
      ),
    );
  }

  Widget _navi(){
    return Container(
      width: double.infinity,
      padding: EdgeInsets.only(left: S.width(20),right: S.width(20),top: S.width(30),bottom: S.width(30)),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          Container(
            decoration: BoxDecoration(
                border: Border.all(color: Color.fromARGB(255, 187, 214, 170),width: 2)///边框颜色、宽
            ),
            child: Row(
              children: <Widget>[
                InkWell(
                  child: Container(
                    margin: EdgeInsets.all(S.width(10)),
                    child: Text("上一章",style: TextStyle(fontWeight: FontWeight.bold,color: Colors.black54),),
                  ),
                  onTap: (){
                    if(_data["PreId"] == 0){
                      Tool.ShowToast("没有数据啦");
                    } else {
                      Navigator.pushNamed(context, "/content",arguments: {
                        "articleId": _data["PreId"],
                        "bookId": widget.arguments["bookId"]
                      });
                    }
                  },
                ),
                SizedBox(width: 2,child: Container(color: Color.fromARGB(255, 187, 214, 170),child: Text(""),),),
                InkWell(
                  child: Container(
                    margin: EdgeInsets.all(S.width(10)),
                    child: Text("回目录",style: TextStyle(fontWeight: FontWeight.bold,color: Colors.black54),),
                  ),
                  onTap: (){
                    Navigator.pushNamed(context, "/book",arguments: {
                      "bookId": widget.arguments["bookId"]
                    });
                  },
                ),
                SizedBox(width: 2,child: Container(color: Color.fromARGB(255, 187, 214, 170),child: Text(""),),),
                InkWell(
                  child: Container(
                    margin: EdgeInsets.all(S.width(10)),
                    child: Text("下一章",style: TextStyle(fontWeight: FontWeight.bold,color: Colors.black54),),
                  ),
                  onTap: (){
                    if(_data["NextId"] == 0){
                      Tool.ShowToast("没有数据啦");
                    } else {
                      Navigator.pushNamed(context, "/content",arguments: {
                        "articleId": _data["NextId"],
                        "bookId": widget.arguments["bookId"]
                      });
                    }
                  },
                ),
                SizedBox(width: 2,child: Container(color: Color.fromARGB(255, 187, 214, 170),child: Text(""),),),
                GestureDetector(
                  child: Container(
                    margin: EdgeInsets.all(S.width(10)),
                    child: Text("存书签",style: TextStyle(fontWeight: FontWeight.bold,color: Colors.black54),),
                  ),
                  onTap: (){
                    _addBookCase(widget.arguments["bookId"],widget.arguments["articleId"],_data["Name"]);
                  },
                ),
              ],
            ),
          )
        ],
      ),
    );
  }
}
